using System;
using NUnit.Framework;
using ValidationFramework.Configuration;
using ValidationFramework.Extensions;

namespace ValidationFramework.Tests
{
    [TestFixture]
    public class CustomRuleConfigReaderTest
    {

        #region Methods

        [Test]
        public void ReadConfig()
        {

            const string validatorXml = "<rule ruleInterpretation='This is a custom rule' ruleSet='foo' validationMethod='ValidateValid' validationTypeName='ValidationFramework.Tests.CustomRuleTest+ValidationClass,ValidationFramework.Tests' errorMessage='hello' typeName='CustomRule'/>";
            var ruleData = RuleData.Read(validatorXml.ToXmlReader());
            var rule = (CustomRule)ConfigurationService.GetRule(ruleData, typeof(bool).TypeHandle);
            Assert.AreEqual("hello", rule.ErrorMessage);
            Assert.AreEqual("This is a custom rule", rule.RuleInterpretation);
            Assert.AreEqual(typeof(CustomRuleTest.ValidationClass).GetMethod("ValidateValid"), rule.Handler.Method);
        }


        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ExceptionInvalidTypeName()
        {
            const string validatorXml = "<rule ruleInterpretation='This is a custom rule' ruleSet='foo' validationMethod='ValidateValid' validationTypeName='InvalidType' errorMessage='hello' typeName='CustomRule'/>";
            var ruleData = RuleData.Read(validatorXml.ToXmlReader());
            ConfigurationService.GetRule(ruleData, typeof(bool).TypeHandle);
        }

        #endregion


    }
}